if '__main__' == __name__:
    xdata = [
        ['小泉纯一郎', '小犬'],
        ['毛泽东', '润之'],
        [None, None],
        ['毛泽东', '毛主席'],
        ['习近平', None],
        ['刘备', '大耳'],
        [None, '习近平'],
        ['刘皇叔', '刘玄德'],
        ['刘备', '刘玄德'],
        ['金三胖', '金正恩'],
        [None, '金正日'],
        ['金正日', '金二胖'],
    ]

    name2set = {}
    for x, y in xdata:
        if x is None and y is None:
            continue
        elif y is None:
            # x is not None
            # 取 x 对应的集合
            set_x = name2set.get(x, None)
            # 如果 x 目前没有映射到集合，则建立映射
            if set_x is None:
                name2set[x] = set([x])
                print(x, '->', set([x]))
            continue
        elif x is None:
            # y is not None
            # 取 y 对应的集合
            set_y = name2set.get(y, None)
            # 如果 y 目前没有映射到集合，则建立映射
            if set_y is None:
                name2set[y] = set([y])
                print(y, '->', set([y]))
            continue

        # x is not None and y is not None
        xpair = set([x, y])
        set_x = name2set.get(x, None)
        set_y = name2set.get(y, None)
        if set_x is None and set_y is None:
            name2set[x] = xpair
            print(x, '->', xpair)
            name2set[y] = xpair
            print(y, '->', xpair)
        elif set_x is None:  # set_y is not None
            name2set[x] = xpair
            print(x, '->', xpair)
            set_x = xpair
        elif set_y is None:  # set_x is not None
            name2set[y] = xpair
            print(y, '->', xpair)
            set_y = xpair

        # 目前 set_x and set_y 都有值
        if id(set_x) != id(set_y):
            set_u = set_x.union(set_y)
            for xname in set_u:
                name2set[xname] = set_u
                print(xname, '->', set_u)

    print('-------------------------------------------------------------------------')

    for name, xset in name2set.items():
        print(name, '->', xset)

    print('-------------------------------------------------------------------------')

    # 去重
    id2sets = {}  # ID-to-set映射
    id_set = set()  # ID的集合
    # 构建
    for name, xset in name2set.items():
        xid = id(xset)
        id2sets[xid] = xset
        id_set.add(xid)

    # 输出
    for xid in id_set:
        print(tuple(id2sets[xid]))
